<?php

namespace Home\Model;
use Think\Model;

/**
 *相册模型
 */
class AlbumModel extends Model
{
	/**
	 * [showAll description]查找UID所有的相册
	 * @param  [type] $id [description]要查相册的UID
	 * @return [type]     [description]
	 */
	public function showAll($id){
		return $this ->where(array('uid' =>$id,'status' =>1)) ->order(array('create_time' =>'desc')) ->select();
	}
	/**
	 * [selectImg description]循环替换相册
	 * @param  [type] $data [description]上面取出来的所有数据
	 * @return [type]       [description]
	 */
	public function selectImg($data){
		foreach ($data as &$value) {
			$value['img'] = $this ->attrImg($value['img']);
		}
		return $data;
	}
	/**
	 * [attrImg description]把字符串该为数组
	 * @param  [type] $img [description]图片的字符串
	 * @return [type]      [description]
	 */
	public function attrImg($img){
		$data = explode(",",$img);
		foreach ($data as $number) {
		$numbers[] = intval(trim($number)); //因为可能数字两侧有空格，所以trim一下
	    }
	    return $numbers;
	}
	/**
	 * [addAlbum description]添加相册
	 * @param [type] $post [description]提交上来的数据
	 */
	public function addAlbum($post){

		$arr['create_time'] = time();
		$arr['uid']         = $post['id'];
		$arr['img']         = $post['imgs'];
		return $this ->add($arr);
	}
	/**
	 * 相册查询
	 */
	public function info($id){
		$map['id'] = $id;
		return $this->where( $map )->find();
	}
	/**
	 * [ajaxDel description]ajax删除
	 * @param  [type] $post [description]
	 * @return [type]       [description]
	 */
	public function ajaxDel($post){
//		foreach ($post as $k => $value) {
//			//先取出相片字段
//			$img_str = $this->where( array('id'=>$k) )->getField('img');
//			$arr = explode(",",$img_str);
//			//删除俩个数组中相同的值
//			$res = array_diff($arr,$value);
//			$str_img = implode(',',$res);
//			//把删除完的字段在添加回去
//			if ($str_img == '') {
//				//如果相册为空，删除此字段
//				$where['id'] = $k;
//				$data = $this ->where($where) ->delete();
//			} else {
//				$data = $this ->where(array('id' =>$k)) ->save(array('img' =>$str_img));
//			}			
//		}
            $where['id'] = array('in',$post['ids']);
            return $this ->where($where) ->delete();
		
	}
	/**
	 * [delNull description]删除图片为空的相册
	 * @param  [type] $data [description]
	 * @return [type]       [description]
	 */
	public function delNull($data){
	foreach ($data as $key => &$value) {
				if($value['img'] == ''){
					$id = $value['id'];
					$this ->where(array('id' =>$id)) ->save(array('status' =>0));
				}
			}
		return $data;
	}

	/**
	 * 发布时间倒序获取用户前4张相册图
	 * @param  [int] $id [用户id]
	 * @return [array]     [图片id]
	 */
	public function limit4($uid){
		$where['uid'] = $uid;
		$where['status'] = 1;
		//存放图片id
		$imgs = array();
		a:
		$data = $this -> where($where) -> order('create_time desc') -> find();
		if (!empty($data)) {
			$imgs = array_merge($imgs,explode(',',$data['img']));
			if (count($imgs) < 4) {
				//如果图片数目不足4张，则继续查询
				$where['create_time'] = array('lt',$data['create_time']);
				goto a;
			}
			return array_slice($imgs,0,4);
		}
		return $imgs;
	}
        public function add_alub(){
        $uid = session('uid');
        $map['status'] = 1;
        $y = date("Y");
        $m = date("m");
        $d = date("d");
        $d1=date("d")+1;
        $todayTime = mktime(0, 0, 0, $m, $d, $y);
        $todayTime1 = mktime(0, 0, 0, $m, "$d1", $y);
        $map['uid']=$uid;
        $map["create_time"]=array(array('gt',$todayTime),array('lt',$todayTime1));
        $return= $this->where($map)->find();
        return $return['img'];
        }
        //更新相册
        public function alub_save($uid,$img){
        $map['status'] = 1;
        $y = date("Y");
        $m = date("m");
        $d = date("d");
        $d1=date("d")+1;
        $todayTime = mktime(0, 0, 0, $m, $d, $y);
        $todayTime1 = mktime(0, 0, 0, $m, "$d1", $y);
        $map['uid']=$uid;
        $map["create_time"]=array(array('gt',$todayTime),array('lt',$todayTime1));
        $return=$this->where($map)->setField('img',$img);
        if($return){
            return TRUE;
        }else{
            return FALSE;
        }
        }
        /*修改后的添加*/
        public function addImgOnt($img){
            $arr['uid'] = session('uid');
            $arr['img'] = $img;
            $arr['create_time'] = time();
            return $this ->add($arr);
        }
}